package android.TaskApp;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class LoginActivity extends Activity {
	
	SharedPreferences prefs;
	
    String password;
    String username;
    TextView tv;
    Toast t_fail;
    Toast t_pass;
    static final int dialogid=20;
	/** Called when the activity is first created. */
    
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
      
        setContentView(R.layout.main);
        Button loginButton;
        Button signupButton;
        Button logoutButton;
   
      
        
        final Intent signupIntent= new Intent(this,SignUpActivity.class);
        final Intent showgroups= new Intent(this,usergroupActivity.class);
        
        loginButton = (Button) findViewById(R.id.loginButton);
        signupButton = (Button) findViewById(R.id.signupButton);
        logoutButton = (Button) findViewById(R.id.logout);
        
        prefs= PreferenceManager.getDefaultSharedPreferences(this);
        final SharedPreferences.Editor editor = prefs.edit();
        
        t_fail = Toast.makeText(this, "Login Failed ! Check Username and Password",Toast.LENGTH_LONG);
	    t_pass = Toast.makeText(this, "Login Successfull", Toast.LENGTH_LONG);   
       
	    
	    
       loginButton.setOnClickListener(new OnClickListener() {
				public void onClick(View v) 
				    {
						showDialog(dialogid);
					    username= prefs.getString("username","");
					    password= prefs.getString("Password","");
					    editor.putInt("dialogid",dialogid);
					    editor.commit();
					    Log.d("username",username);
					    Log.d("password",password);
					    
					   if(username=="")
					    {
						   startActivity(new Intent(LoginActivity.this,PrefsActivity.class));
					    }  
				        UserResponse response = WebServiceHelper.UserLogin(username,password); 
				        UserVO[] user= response.getUsers();
				        dismissDialog(dialogid);
				        if(response.getStatusCode()>0)
				        {
				          int i=user[0].getUserId();
					      t_pass.show();
				          editor.putInt("userid",i);
				          editor.commit();
				          //dismissDialog(dialogid);
				          startActivity(showgroups);
				          
				        }
				        else 
				        { 
				        	startActivity(new Intent(LoginActivity.this,PrefsActivity.class));					    
				            t_fail.show(); 
				        }
					}
			});
             
      signupButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) 
			  {
				startActivity(signupIntent);
			  }
			});

	  logoutButton.setOnClickListener(new View.OnClickListener() {
		public void onClick(View v) {
		
			SharedPreferences.Editor editor= prefs.edit();
			editor.putString("username","");
			editor.putString("Password","");
			editor.putInt("userid", 0);
			editor.putInt("homegroupid", 0);
			editor.commit();
			Toast.makeText(LoginActivity.this,"Credentials Cleared", Toast.LENGTH_LONG).show();
		}
	});
	}


	
	@Override
	protected Dialog onCreateDialog(int id) {
		switch(id)
		{
		case dialogid:
		   { 
			ProgressDialog dialog= new ProgressDialog(this);
			dialog.setTitle("TaskMan Login");
			dialog.setMessage("Please wait while you login");
			dialog.setIndeterminate(true);
			dialog.setCancelable(true);
			return dialog;
		  }
		}
		return null;
	}

	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) 
		{
		getMenuInflater().inflate(R.menu.prefsmenu,menu);
		return true;
		}
	

	@Override
	public boolean onOptionsItemSelected(MenuItem item) 
		{
		 switch(item.getItemId())
		 {
		
		  case R.id.login_item:
				   Intent i=new Intent(this,PrefsActivity.class);
		           startActivity(i);
		           break;
		 }
		return true;
		}        
	}